package wkday12.demo02;

public class BaoZiPu extends Thread {
    private final BaoZi bz;

    BaoZiPu(String name, BaoZi bz) {
        super(name);
        this.bz = bz;
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
            synchronized (bz) {
                // 有包子
                if (bz.flag) {
                    try {
                        // 等待包子被卖
                        System.out.println(Thread.currentThread().getName() + " 等待吃货吃包子");
                        bz.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    // 没包子
                    System.out.print(Thread.currentThread().getName() + "开始做包子   ...   ");
                    // 生产包子
                    if (count % 2 == 0) {
                        bz.pi = "冰皮";
                        bz.xian = "大葱";
                    } else {
                        bz.pi = "薄皮";
                        bz.xian = "牛肉";
                    }
                    count++;
                    // 包子做好了
                    bz.flag = true;
                    System.out.println(Thread.currentThread().getName() + bz.pi + bz.xian + "包子做好了");
                    // 唤醒吃货吃包子
                    bz.notifyAll();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
